package io.kimmking.javacourse.mq.activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

import javax.jms.*;


public class ActivemqProducer {

    public static void main(String[] args) {

        // 定义Destination
        Destination destination = new ActiveMQTopic("test.topic");
//        Destination destination = new ActiveMQQueue("test.queue");

        producer(destination);

    }

    public static void producer(Destination destination) {
        try {
            // 创建连接和会话
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.1.59:61616");
            ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection();
            conn.start();
            Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);


            // 创建生产者，生产100个消息
            MessageProducer producer = session.createProducer(destination);
            int index = 0;
            while (index++ < 100000) {
                TextMessage message = session.createTextMessage(index + " message.");
                producer.send(message);
            }

//            Thread.sleep(20000);
//            session.close();
//            conn.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
